// 导入需要的config和request
import confing from '../config/config.js';
import request from '../util/request.js';
//申明请求对象
const defectRequest = {

    //defect请求的基本url
    BASE_URL: confing.QC_URL + "/defect",
    /**
     * 保存常见缺陷信息
     * @param {*} defect 
     * @param {*} callback 
     */
    save(defect, callback) {
        if (defect) {
            request.post(this.BASE_URL + "/save", defect, callback);
        }
    },


    /**
        * 根据defectCode编辑缺陷信息
        * @param {*} defect 
        * @param {*} callback 
        */
    update(defect, callback) {
        if (defect != undefined && defect.defectCode != undefined) {
            request.post(this.BASE_URL + "/update", defect, callback);
        }
    },
    /**
        * 根据defectCode加载常见缺陷信息
        * @param {*} defectCode 
        * @param {*} callback 
     */
    get(defectCode, callback) {
        if (defectCode) {
            let url = this.BASE_URL + "/get?defectCode=" + defectCode;
            request.get(url, callback);
        }
    },
    /**
      * 根据defectCode加载常见缺陷信息
      * @param {*} defectCode 
      * @param {*} callback 
   */
    delete(defectCode, callback) {
        if (defectCode) {
            let url = this.BASE_URL + "/delete?defectCode=" + defectCode;
            request.delete(url, callback);
        }
    },

    /**
      * 根据deleteDefectCodes加载常见缺陷信息
      * @param {*} deleteDefectCodes 
      * @param {*} callback 
   */
    deletes(deleteDefectCodes, callback) {
        let url = this.BASE_URL + "/deletes?t=1";
        if (deleteDefectCodes != undefined) {
            deleteDefectCodes.forEach(defectCode => {
                url += "&defectCode=" + defectCode;
            });
            request.delete(url, callback);
        }
    },

    //请求的API
    list(page = 1, limit = 10, defect, callback) {
        //url
        let url = this.BASE_URL + "/list?page=" + page + "&limit=" + limit;
        if (defect) {
            if (defect.defectName) {
                url += "&defectName=" + defect.defectName;
            }
            if (defect.defectCode) {
                url += "&defectCode=" + defect.defectCode;
            }
            if (defect.indexType) {
                url += "&indexType=" + defect.indexType;
            }
            if (defect.defectLevel) {
                url += "&defectLevel=" + defect.defectLevel;
            }

        }
        //发送请求
        request.get(url, callback);

    }
}

//将defectRequest暴漏出去
export default defectRequest;